package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.TSchool;
import model.VCloudSchoolAnecdoteStatis;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import annotation.Log;
import persistent.factory.DAOFactory;
import util.toolkit.Constant;
import util.toolkit.Expression;
import util.toolkit.ResponseJSON;

/**
 * 轶事统计处理类，学校，年级，班级
 * @author 徐震霖
 * @since 2018-11-18 17:10
 *
 */
@Controller
@RequestMapping(value="/sysanecdotestatis")
public class SysAnecdoteStatisController {

	/**
	 * 班级轶分析列表查询控制类
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 班级轶分析列表查询控制类
	/*@RequestMapping(value="/getclassanecdotenumber")
	public String getClassAnecdoteNumberList(Model model, HttpServletRequest req, HttpServletResponse resp) throws IOException{
		
		req.setCharacterEncoding("utf-8");		
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		HttpSession session = req.getSession();
		Expression exp = new Expression();		
		
		try {		
			String gradename=req.getParameter("gradename");		
			String page = req.getParameter("page");    //当前页 
			String limit = req.getParameter("limit");  //每页条数

			String schoolid=req.getParameter("schoolid");
			exp.andEqu("schoolid", schoolid, String.class);
			if(gradename!=null && !gradename.equals("")){
				exp.andLike("gradename", gradename, String.class);
			}
			String strwhere=exp.toString();
			List<VCloudClassAnecdoteNumber> list=DAOFactory.getAnecdoteStatisDAO().getClassAnecdoteNumber(strwhere, Integer.parseInt(page), Integer.parseInt(limit));
			List<VCloudClassAnecdoteNumber> listsize=DAOFactory.getAnecdoteStatisDAO().getClassAnecdoteNumber(strwhere);
			if(list!=null && list.size()>0){
				result.put("code", 0);
				result.put("msg", "查询成功！");
				result.put("count", listsize.size());
				JSONArray jsonArray = JSONArray.fromObject(list);
				result.element("data", jsonArray);
			}else{
				result.put("code", 1);
				result.put("msg", "未获取到数据！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", 1);
			result.put("msg", "出现异常，请重试！");
		}
		
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
	}*/
	
	/**
	 * 班级轶事分析，维度详情,表格数据
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException 
	 */
	/*//TODO 班级轶事分析，维度详情,表格数据
	@RequestMapping(value="/getclassanecdotestatis")
	public String getClassAnecdoteStatisList(Model model, HttpServletRequest req, HttpServletResponse resp) throws IOException{
		
		req.setCharacterEncoding("utf-8");		
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		HttpSession session = req.getSession();
		Expression exp = new Expression();
				
		try {
			VCloudUser user = (VCloudUser) session.getAttribute("cloudUser");		
			String classid = req.getParameter("classid");
			String page = req.getParameter("page");    //当前页 
			String limit = req.getParameter("limit");  //每页条数
			
			if(classid != null && !classid.equals("") && page != null && !page.equals("") && limit != null && !limit.equals("") && user != null){
				exp.andEqu("schoolid", user.getSchoolid(), String.class);
				exp.andEqu("classid", classid, String.class);
				String strwhere=exp.toString();
				List<VCloudClassAnecdoteStatis> list=DAOFactory.getAnecdoteStatisDAO().getClassAnecdoteStatisByPage(strwhere, Integer.parseInt(page), Integer.parseInt(limit));
				if(list!=null && list.size()>0){
					result.put("code", 0);
					result.put("msg", "查询成功!");
					result.put("count", list.size());
					JSONArray jsonArray = JSONArray.fromObject(list);
					result.element("data", jsonArray);
				}else{
					result.put("code", 1);
					result.put("msg", "无查询结果!");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", 1);
			result.put("msg", "出现异常，请重试！");
		}
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
	}*/

	/**
	 * 班级轶事分析，维度详情,表格数据(不分页，用于加载班级轶事报告打印)
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException 
	 */
	//TODO 班级轶事分析，维度详情,表格数据(不分页，用于加载班级轶事报告打印)
	/*@RequestMapping(value="/getclassanecdotestatisnotpage")
	public String getClassAnecdoteStatisListNotPage(Model model, HttpServletRequest req, HttpServletResponse resp) throws IOException{
		
		req.setCharacterEncoding("utf-8");		
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		HttpSession session = req.getSession();
		Expression exp = new Expression();
		
		try {
			VCloudUser user = (VCloudUser) session.getAttribute("cloudUser");		
			String classid=req.getParameter("classid");
			
			if(classid!=null && !classid.equals("") && user!=null){
				exp.andEqu("schoolid", user.getSchoolid(), String.class);
				exp.andEqu("classid", classid, String.class);
				String strwhere = exp.toString();
				List<VCloudClassAnecdoteStatis> list = DAOFactory.getAnecdoteStatisDAO().getClassAnecdoteStatis(strwhere);
				if(list!=null && list.size()>0){
					result.put("code", 0);
					result.put("msg", "查询成功！");
					result.put("count", list.size());
					JSONArray jsonArray = JSONArray.fromObject(list);
					result.element("data", jsonArray);
				}else{
					result.put("code", 1);
					result.put("msg", "无查询结果！");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", 1);
			result.put("msg", "出现异常，请重试！");
		}
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
	}*/
	
	/**
	 * 获取学校信息处理类
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 获取学校信息处理类
	@Log(operationType = Constant.OPERTATION_QUERY, operationDescribe = "查询学校信息", 
			operationClassPath = "controller.SysAnecdoteStatisController.getSchoolList(Model model, HttpServletRequest req, HttpServletResponse resp)", 
			operationMenu = "学校信息管理/学校信息管理")
	@RequestMapping(value="/getschoollist")
	public String getSchoolList(Model model, HttpServletRequest req, HttpServletResponse resp) throws IOException {
		
		req.setCharacterEncoding("utf-8");		
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		Expression exp=new Expression();
		
		int currentPage = 1;
		int pageSize = 10;
		try {
			String schoolid=req.getParameter("schoolid");
			String limit=req.getParameter("limit");
			String page=req.getParameter("page");
			
			if(schoolid!=null && !schoolid.equals("")){
				exp.andEqu("schoolid", schoolid, String.class);
			}
			if(page!=null && !page.equals("")){
				currentPage=Integer.parseInt(page);
			}
			if(limit!=null && !limit.equals("")){
				pageSize=Integer.parseInt(limit);
			}
			
			List<TSchool> list = DAOFactory.getSchoolDAO().getSchoolByCondition
					(exp.toString(), currentPage, pageSize);
			List<TSchool> listsize = DAOFactory.getSchoolDAO().getSchoolByCondition
					(exp.toString());
			if(list != null && list.size()>0 && listsize.size()>0){
				result.put("code", 0);
				result.put("msg", "查询成功");
				result.put("count", listsize.size());
				JSONArray jsonArray = JSONArray.fromObject(list);
				result.element("data", jsonArray);
			}
			else {
				result.put("code", 1);
				result.put("msg", "未获取到学校信息！");
			}
		} catch (Exception e) {
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "出现异常，学校信息获取失败，请重试！");
			e.printStackTrace();
		}
		
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
		
	}
	
	/**
	 * 根据学校id获取轶事占比信息处理类
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 根据学校id获取轶事占比信息处理类
	@Log(operationType = Constant.OPERTATION_QUERY, operationDescribe = "根据学校id获取轶事占比", 
			operationClassPath = "controller.SysAnecdoteStatisController.getAnecdoteStatis(Model model, HttpServletRequest req, HttpServletResponse resp)", 
			operationMenu = "轶事管理/分析图表查看")
	@RequestMapping(value="/getanecdotestatis")
	public String getAnecdoteStatis(Model model, HttpServletRequest req, HttpServletResponse resp) throws IOException {
		
		req.setCharacterEncoding("utf-8");		
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		Expression exp = new Expression();
		
		try {
			String schoolid = req.getParameter("schoolid");
			
			if (schoolid != null && !schoolid.equals("")) {
				exp.andEqu("schoolid", schoolid, String.class);
				String strWhere = exp.toString();
				List<VCloudSchoolAnecdoteStatis> list = DAOFactory.getAnecdoteStatisDAO().getSchoolAnecdoteStatis(strWhere);
				if (list != null && list.size() > 0) {
					result.put("code", 0);
					result.put("msg", "查询成功！");
					result.put("count", list.size());
					JSONArray jsonArray = JSONArray.fromObject(list);
					result.element("data", jsonArray);
				}
				else {
					result.put("code", ResponseJSON.FLAG_FAIL);
					result.put("msg", "未获取到信息，请重试！");
				}
			}			
			else {
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "学校编号不能为空，未获取到信息！");
			}
		} catch (Exception e) {
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "出现异常，信息获取失败，请重试！");
			e.printStackTrace();
		}
		
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
		
	}
	
	/**
	 * 获取年级信息处理类
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 根据学校id和年级名称获取轶事占比信息处理类
	/*@RequestMapping(value="/getanecdotestatisbygrade")
	public String getAnecdoteStatisByGrade(Model model, HttpServletRequest req, HttpServletResponse resp) throws IOException {
		
		req.setCharacterEncoding("utf-8");		
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		Expression exp = new Expression();
		
		try {
			String schoolid = req.getParameter("schoolid");
			String gradeName = req.getParameter("gradeName");
			if (schoolid != null && !schoolid.equals("") && gradeName != null && !gradeName.equals("")) {
				exp.andEqu("schoolid", schoolid, String.class);
				exp.andEqu("gradename", gradeName, String.class);
				String strWhere = exp.toString();
				List<VCloudGradeAnecdoteStatis> list = DAOFactory.getAnecdoteStatisDAO().getGradeAnecdoteStatis(strWhere);
				if (list != null && list.size() > 0) {
					result.put("code", 0);
					result.put("msg", "查询成功");
					result.put("count", list.size());
					JSONArray jsonArray = JSONArray.fromObject(list);
					result.element("data", jsonArray);
				}
				else {
					result.put("code", ResponseJSON.FLAG_FAIL);
					result.put("msg", "未获取到信息，请重试！");
				}
			}			
			else {
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "学校编号不能为空，未获取到信息！");
			}
		} catch (Exception e) {
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "出现异常，信息获取失败，请重试！");
			e.printStackTrace();
		}
		
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
		
	}*/
	
	/**
	 * 获取年级信息处理类
	 * @param model
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	//TODO 获取学校信息处理类
	/*@RequestMapping(value="/getgradelist")
	public String getGradeList(Model model, HttpServletRequest req, HttpServletResponse resp) throws IOException {
		
		req.setCharacterEncoding("utf-8");		
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("application/json");
		
		JSONObject result = new JSONObject();
		HttpSession session = req.getSession();
		Expression exp = new Expression();
		
		try {
			VCloudUser user = (VCloudUser) session.getAttribute("cloudUser");
			exp.andEqu("schoolid", user.getSchoolid(), String.class);
			List<VCloudGradeAnecdoteStatis> gradeList = DAOFactory.getAnecdoteStatisDAO().getGradeAnecdoteStatis(exp.toString());
			if(gradeList != null && gradeList.size() > 0){
				result.put("code", 0);
				result.put("msg", "查询成功");
				result.put("count", gradeList.size());
				JSONArray jsonArray = JSONArray.fromObject(gradeList);
				result.element("data", jsonArray);
			}
			else {
				result.put("code", ResponseJSON.FLAG_FAIL);
				result.put("msg", "未获取到年级信息！");
			}
		} catch (Exception e) {
			result.put("code", ResponseJSON.FLAG_FAIL);
			result.put("msg", "出现异常，年级信息获取失败，请重试！");
			e.printStackTrace();
		}
		
		PrintWriter out = resp.getWriter();
		out.println(result);
		return null;
		
	}*/
}
